<!doctype html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
  <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
  <title>设置</title>
  <link rel="stylesheet" type="text/css" href="../css/api.css" />
  <link rel="stylesheet" type="text/css" href="../css/setting.css" />
</head>

<body>
  <div id="app" v-cloak>
    <div class="wrap flex-wrap flex-vertical">
      <header id="header" class="border-bottom">
        <div class="header-wrap">
          <div @click="goback" class="header-left">
            <i class="iconfont icon-pre"></i>
          </div>
          <div class="header-title">设置</div>
        </div>
      </header>
      <div class="list border-topbottom">
        <span class="label">手机</span>
        <span class="toright">{{mobile}}</span>
      </div>
      <!-- <div class="list border-topbottom" v-if="mobile == ''" @click='openWeixin'>
        <span class="label">手机</span>
        <span class="toright">{{mobile}}</span>
      </div> -->
      <!-- <div class="list border-bottom">
        <span class="label">勿扰</span>
        <div id="toggle-button" class="toggle-button"></div>
      </div> -->
      <!-- <div class="list border-bottom">
        <span class="label">官方QQ</span>
        <span class="toright">
          <i class="iconfont icon-qianjinxiayige"></i>
        </span>
      </div> -->
      <!-- <div class="list border-bottom">
        <span class="label">联系我们</span>
        <span class="toright">
          <i class="iconfont icon-qianjinxiayige"></i>
        </span>
      </div> -->
      <div @click="clearCache " class="list border-bottom">
        <span class="label">清理缓存</span>
        <span class="toright">
          {{cacheSize}}MB
          <i class="iconfont icon-qianjinxiayige"></i>
        </span>
      </div>
      <div class="list border-bottom" @click="checkUpdate">
        <span class="label">版本号</span>
        <span class="toright">
          {{appVersion}}
        </span>
      </div>
      <footer>
        <button @click="logout" type="button">退出登录</button>
      </footer>
    </div>
  </div>
</body>

</html>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/vue.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript">
  var app, switchBtn;
  apiready = function () {
    $api.fixStatusBar($api.dom('#header'));
    // switchBtn = api.require('UISwitchButton');
    api.setStatusBarStyle({
      style: 'light'
    });
    app = new Vue({
      el: '#app',
      data: {
        mobile: '',
        appVersion: api.appVersion,
        cacheSize: 0,
        status: false
      },
      created: function () {
        var _this = this;
        _this.status = api.pageParam.status == 0 ? false : true;
        api.getCacheSize(function (ret) {
          _this.cacheSize = (parseInt(ret.size) / 1024 / 1024).toFixed(2);
        });
        if (!!$api.getStorage('umobile')) {
          _this.mobile = $api.getStorage('umobile').length > 11 ? '' : $api.getStorage('umobile')
        }
      },
      mounted: function () {
        var _this = this;
        // var toggleButton_offset = $api.offset($api.byId('toggle-button'));
        // switchBtn.open({
        //   id: '1',
        //   state: !_this.status,
        //   rect: {
        //     x: toggleButton_offset.l,
        //     y: toggleButton_offset.t,
        //     w: toggleButton_offset.w,
        //     h: toggleButton_offset.h
        //   },
        //   styles: {
        //     bgCorner: 10,
        //     active: '#eee',
        //     inActive: '#eee',
        //     thumb: {
        //       active: '#e12e85',
        //       inActive: '#ccc',
        //       size: 20,
        //       corner: 10
        //     }
        //   },
        //   fixed: true
        // }, function (ret) {
        //   api.ajax({
        //     url: baseUrl + 'oauth/logout',
        //     method: 'get',
        //     headers: {
        //       token: $api.getStorage('deviceToken')
        //     },
        //     data: {
        //       values: {
        //         uid: $api.getStorage('uid')
        //       },
        //     }
        //   }, function (ret, err) {
        //     if (ret && ret.status == 200) {
        //       if (ret.data.status == 0) {
        //         api.toast({
        //           msg: '勿扰打开'
        //         })
        //       } else {
        //         api.toast({
        //           msg: '勿扰关闭'
        //         })
        //       }
        //     }
        //   });
          
        // });
      },
      methods: {
        openWeixin:function(){
          api.openWin({
            name: 'weixin_phone',
            url:'weixin_phone.html',
            pageParam: {
              show: false
            }
          });
        },
        
        goback: function () {
          api.closeWin();
        },

        toggleStatus: function () {

        },

        // 清除缓存
        clearCache: function () {
          var _this = this;
          api.confirm({
            title: '提醒',
            msg: '确定清除缓存?',
            buttons: ['确定', '取消']
          }, function (ret, err) {
            if (ret.buttonIndex == 1) {
              api.showProgress({
                title: '',
                text: ''
              });
              api.clearCache(function () {
                api.hideProgress();
                _this.cacheSize = 0;
                api.toast({
                  msg: '清除完成'
                });
              });
            }
          });
        },

        logout: function () {
          var rong = api.require('rongCloud2');
          api.ajax({
            url: baseUrl + 'oauth/logout',
            method: 'get',
            headers: {
              token: $api.getStorage('deviceToken')
            },
            data: {
              values: {
                uid: $api.getStorage('uid')
              },
            }
          }, function (ret, err) {
            if (ret && ret.status == 200) {
              $api.rmStorage('uid');
              $api.rmStorage('uname');
              $api.rmStorage('uavatar');
              $api.rmStorage('umobile');
              $api.rmStorage('token');
              $api.rmStorage('index');
              rong.disconnect({
                isReceivePush: true
              }, function (ret, err) {
                if (ret.status == 'success') {
                  api.toast({
                    msg: '退出登录'
                  });
                }
                setTimeout(function () {
                  api.closeToWin({
                    name: 'root'
                  });
                }, 1000);
              });
            }
          });
        },

        // 检查app版本更新
        checkUpdate: function () {
          var mam = api.require('mam');
          mam.checkUpdate(function (ret, err) {
            if (ret) {
              var result = ret.result;
              if (result.update) {
                var str = '新版本号:' + result.version + ';\n更新内容:\n' + result.updateTip + ';\n发布时间:' + result.time;
                if (result.closed) {
                  api.alert({
                    title: '有新版本更新',
                    msg: str,
                  }, function (ret, err) {
                    if (ret.buttonIndex == 1) {
                      if (api.systemType == "android") {
                        api.download({
                          url: result.source,
                          report: true
                        }, function (ret, err) {
                          if (ret && 0 == ret.state) {
                            api.showProgress({
                              title: ret.percent + "%",
                              text: ''
                            });
                          }
                          if (ret && 1 == ret.state) {
                            api.hideProgress();
                            api.installApp({
                              appUri: ret.savePath
                            });
                          }
                        });
                      }
                      if (api.systemType == "ios") {
                        api.installApp({
                          appUri: result.source
                        });
                      }
                    }
                  });
                } else {
                  api.confirm({
                    title: '有新版本推送，是否下载安装',
                    msg: str,
                    buttons: ['确定', '下次再说']
                  }, function (ret, err) {
                    if (ret.buttonIndex == 1) {
                      if (api.systemType == "android") {
                        api.download({
                          url: result.source,
                          report: true
                        }, function (ret, err) {
                          if (ret && 0 == ret.state) {
                            api.showProgress({
                              title: ret.percent + "%",
                              text: ''
                            });
                          }
                          if (ret && 1 == ret.state) {
                            api.hideProgress();
                            api.installApp({
                              appUri: ret.savePath
                            });
                          }
                        });
                      }
                      if (api.systemType == "ios") {
                        api.installApp({
                          appUri: result.source
                        });
                      }
                    }
                  });
                }
              } else {
                if (result.closed) {
                  // 无更新，强制关闭
                  api.alert({
                    title: '无新的版本',
                    msg: '应用已强制关闭',
                  }, function (ret, err) {
                    if (ret.buttonIndex == 1) {
                      api.closeWidget({
                        id: api.appId,
                        silent: true
                      });
                    }
                  });
                } else {
                  api.toast({
                    msg: '应用已是最新版本',
                  });
                }
              }
            }
          });
        },
      },
    })
  }
</script>